<!-- 内部便签组件 -->
<template>
  <div class="internal-notes">
    <div class="internal-notes-header">
      <el-button type="primary" size="small" @click="handleAdd">新增便签</el-button>
    </div>

    <el-table
      :data="notesList"
      stripe
      border
      size="small"
      v-loading="loading"
    >
      <el-table-column prop="noteName" label="便签名称" align="center" width="200" />
      <el-table-column prop="operator" label="操作人" align="center" width="150" />
      <el-table-column prop="operateTime" label="操作时间" align="center" width="180">
        <template slot-scope="scope">
          {{ formatDate(scope.row.operateTime) }}
        </template>
      </el-table-column>
      <el-table-column prop="content" label="便签内容" align="center" show-overflow-tooltip />
    </el-table>

    <!-- 新增便签弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="便签名称" prop="noteName">
          <el-input v-model="form.noteName" placeholder="请输入便签名称" />
        </el-form-item>
        <el-form-item label="便签内容" prop="content">
          <el-input type="textarea" :rows="4" v-model="form.content" placeholder="请输入便签内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'InternalNotes',
  props: {
    cellId: {
      type: [String, Number],
      default: null
    }
  },
  data() {
    return {
      loading: false,
      notesList: [],
      open: false,
      title: '',
      form: {
        noteName: '',
        content: ''
      },
      rules: {
        noteName: [
          { required: true, message: '便签名称不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '便签内容不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    cellId: {
      handler(newVal) {
        if (newVal) {
          this.getNotesList()
        }
      },
      immediate: true
    }
  },
  methods: {
    // 获取便签列表
    getNotesList() {
      if (!this.cellId) return

      this.loading = true
      // 模拟接口调用
      setTimeout(() => {
        this.notesList = [
          {
            id: 1,
            noteName: '客户特殊要求',
            operator: '张三',
            operateTime: '2024-01-15 10:30:00',
            content: '客户要求包装时加入特殊标识，注意处理'
          },
          {
            id: 2,
            noteName: '发货注意事项',
            operator: '李四',
            operateTime: '2024-01-14 14:20:00',
            content: '该订单需要使用特殊包装材料，避免损坏'
          }
        ]
        this.loading = false
      }, 500)

      // TODO: 替换为实际API调用
      // getInternalNotes(this.cellId).then(response => {
      //   this.notesList = response.data
      //   this.loading = false
      // }).catch(() => {
      //   this.loading = false
      // })
    },

    // 新增便签
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增便签'
    },

    // 提交表单
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // TODO: 调用新增接口，添加操作人和操作时间
          this.$message.success('新增成功')
          this.open = false
          this.getNotesList()
        }
      })
    },

    // 取消
    cancel() {
      this.open = false
      this.reset()
    },

    // 重置表单
    reset() {
      this.form = {
        noteName: '',
        content: ''
      }
      if (this.$refs.form) {
        this.$refs.form.clearValidate()
      }
    },

    // 格式化日期
    formatDate(date) {
      if (!date) return ''
      const d = new Date(date)
      return d.getFullYear() + '-' + 
             String(d.getMonth() + 1).padStart(2, '0') + '-' + 
             String(d.getDate()).padStart(2, '0') + ' ' +
             String(d.getHours()).padStart(2, '0') + ':' + 
             String(d.getMinutes()).padStart(2, '0') + ':' + 
             String(d.getSeconds()).padStart(2, '0')
    }
  }
}
</script>

<style scoped>
.internal-notes {
  padding: 10px;
}

.internal-notes-header {
  margin-bottom: 10px;
}

.internal-notes-header .el-button {
  margin-right: 10px;
}
</style>
